﻿using BackupContracts.Sql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight
{
    public class EditSqlServerDialogResult
    {
        public SqlServer SqlServer { get; set; }
    }

    public partial class EditSqlServerDialog : ChildWindow
    {
        readonly AutoResetEvent waitHandle;
        EditSqlServerDialogResult result;

        public EditSqlServerDialog()
        {
            InitializeComponent();
            waitHandle = new AutoResetEvent(false);
        }

        public static Task<EditSqlServerDialogResult> ShowModal(SqlServer sqlServer)
        {
            var dialog = new EditSqlServerDialog();
            dialog.Show();
            dialog.DataContext = sqlServer;
            dialog.Closed += (s, e) => dialog.waitHandle.Set();
            return Task<EditSqlServerDialogResult>.Factory.StartNew(() =>
            {
                dialog.waitHandle.WaitOne(); 
                return dialog.result;
            });
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            result = new EditSqlServerDialogResult() 
            {
                SqlServer = DataContext as SqlServer,
            };
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}

